package mh.map;

import java.io.IOException;

import mh.GameWindow;
import mh.interfaces.IImage;

import org.lwjgl.opengl.GL11;

/**
 * The image shown when a spell is created.<br>
 * Rotation and animation are included.
 * 
 * @author Gianmarco Laggia
 * 
 */
public class SpellImage implements IImage {

	/**
	 * The actual image linked to this sprite. Many tiles are included in the same file.
	 */
	private Texture	texture;
	/**
	 * The angle that is set to the sprite.
	 */
	private int		angle;

	/**
	 * Standard constructor. Uses a <code>GameWindow</code> object to get a <code>Texture</code>
	 * 
	 * @param window
	 *            the windows to get the texture
	 * @param ref
	 *            path of the imageset
	 */
	public SpellImage(GameWindow window, String ref) {
		try {
			GameWindow.getTextureLoader();
			this.texture = TextureLoader.getTexture(ref, false, null);
		} catch (IOException e) {
			System.err.println("Unable to load texture: " + ref);
		}
	}

	/**
	 * Get the width of this sprite in pixels
	 * 
	 * @return The width of this sprite in pixels
	 */
	@Override
	public int getWidth() {
		return this.texture.getImageWidth();
	}

	/**
	 * Get the height of this sprite in pixels
	 * 
	 * @return The height of this sprite in pixels
	 */
	@Override
	public int getHeight() {
		return this.texture.getImageHeight();
	}

	/**
	 * @return the angle
	 */
	public int getAngle() {
		return this.angle;
	}

	/**
	 * Draw the sprite at the specified location
	 * 
	 * @param x
	 *            The x location where to draw this sprite (tile unit)
	 * @param y
	 *            The y location where to draw this sprite (tile unit)
	 */
	@Override
	public void draw(double x, double y) {
		// store the current model matrix
		GL11.glPushMatrix();

		// bind to the appropriate texture for this sprite
		this.texture.bind();

		// translate to the right location and prepare to draw
		GL11.glColor3f(1, 1, 1);
		GL11.glTranslated(x + ((32 - this.texture.getImageWidth()) / 2) + (this.texture.getImageWidth() / 2), y + ((32 - this.texture.getImageHeight()) / 2)
				+ (this.texture.getImageHeight() / 2), 0);
		//		System.out.println(this.angle);
		GL11.glRotated(this.angle, 0, 0, 1);
		GL11.glTranslated(-this.texture.getImageWidth() / 2, -this.texture.getImageHeight() / 2, 0);
		// draw a quad textured to match the sprite
		GL11.glBegin(GL11.GL_QUADS);
		{
			GL11.glTexCoord2f(0, 0);
			GL11.glVertex2f(0, 0);
			GL11.glTexCoord2f(0, this.texture.getHeight());
			GL11.glVertex2f(0, this.texture.getImageHeight());
			GL11.glTexCoord2f(this.texture.getWidth(), this.texture.getHeight());
			GL11.glVertex2f(this.texture.getImageWidth(), this.texture.getImageHeight());
			GL11.glTexCoord2f(this.texture.getWidth(), 0);
			GL11.glVertex2f(this.texture.getImageWidth(), 0);
		}
		GL11.glEnd();

		// restore the model view matrix to prevent contamination
		GL11.glPopMatrix();
	}

	/**
	 * Sets the slope at wich the spell is aimed.
	 * 
	 * @param d
	 *            the angle to set (in degrees)
	 */
	@Override
	public void setDirection(int d) {
		this.angle = d;
	}

	@Override
	public void setFrame(int f) {
		// TODO Auto-generated method stub

	}

	@Override
	public void frameUp() {
		// TODO Auto-generated method stub

	}

}
